package team.seekers.hula.course.resource.problem.service;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import team.seekers.hula.common.entity.PageResult;
import team.seekers.hula.common.entity.result.ApiExceptionEnum;
import team.seekers.hula.common.utils.AssertUtils;
import team.seekers.hula.course.resource.problem.dao.ProblemDao;
import team.seekers.hula.course.resource.problem.entity.dao.ProblemDO;
import team.seekers.hula.course.resource.problem.entity.dto.ProblemDTO;
import team.seekers.hula.course.resource.problem.entity.dto.ProblemPageQueryDTO;
import team.seekers.hula.course.resource.problem.entity.vo.ProblemListVO;
import team.seekers.hula.course.resource.problem.entity.vo.ProblemPageListVO;
import team.seekers.hula.course.resource.problem.entity.vo.ProblemVO;
import team.seekers.hula.user.user.entity.dto.UserInfoDTO;
import team.seekers.hula.user.user.service.UserService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * date: 2022/5/2 17:24
 * @author GH
 * QQ 1124813753
 */
@Service
public class ProblemService {

    @Autowired
    private ProblemDao problemDao;

    @Autowired
    private UserService userService;


    public String saveOrUpdate(ProblemDTO problemDTO) {
        ProblemDO problemDO = ProblemDO.fromDTO(problemDTO);
        problemDao.saveOrUpdate(problemDO);
        return problemDO.getId();
    }

    public void delete(Long problemId, String userId) {
        AssertUtils.isTrue(isProblemManager(problemId, userId),
                ApiExceptionEnum.USER_NO_PERMISSION, "非问题创建人");
        AssertUtils.isTrue(!problemDao.removeById(problemId), ApiExceptionEnum.DB_FAIL);
    }

    public List<ProblemDTO> queryByIds(List<String> problemIds) {
        if(problemIds.size() == 0){
            return new ArrayList<>();
        }
        List<ProblemDO> problemDOList = problemDao.lambdaQuery()
                .in(ProblemDO::getId, problemIds).list();
        return ProblemDO.toListDTO(problemDOList);
    }

    public boolean isProblemManager(Long problemId, String userId) {
        // TODO 需要添加管理员认证
        return userId.equals(problemDao.getById(problemId).getUserId());
    }

    public List<ProblemListVO> list(String description) {
        LambdaQueryChainWrapper<ProblemDO> lambdaQuery = problemDao.lambdaQuery().select(
                ProblemDO::getId,
                ProblemDO::getDescription,
                ProblemDO::getType
        );
        if(StringUtils.isNotBlank(description)){
            lambdaQuery.likeRight(ProblemDO::getDescription,description);
        }
        Page<ProblemDO> page = lambdaQuery.page(new Page<>(1, 10));

        ArrayList<ProblemListVO> problemListVOS = new ArrayList<>();
        for (ProblemDO record : page.getRecords()) {
            ProblemListVO problemPageListVO = new ProblemListVO();
            BeanUtils.copyProperties(record,problemPageListVO);
            problemListVOS.add(problemPageListVO);
        }
        return problemListVOS;
    }

    public PageResult<ProblemPageListVO> page(ProblemPageQueryDTO dto) {
        LambdaQueryChainWrapper<ProblemDO> lambdaQuery = problemDao.lambdaQuery();
        if(StringUtils.isNotBlank(dto.getDescription())){
            lambdaQuery.likeRight(ProblemDO::getDescription,dto.getDescription());
        }
        Page<ProblemDO> page = lambdaQuery.page(new Page<>(dto.getPageNow(), dto.getPageSize()));
        if(page.getRecords().size() < 1){
            return new PageResult<>();
        }

        // 获取所有题目创建者的id和name的map
        ArrayList<String> userIds = new ArrayList<>();
        for (ProblemDO record : page.getRecords()) {
            userIds.add(record.getUserId());
        }
        AssertUtils.isTrue(userIds.size()>0,ApiExceptionEnum.UNKNOWN_ERROR,"部分题目没有创建者id");
        Map<String, UserInfoDTO> longUserBasicInfoDTOMap = userService.userIdsToUserInfoDTOs(userIds);

        ArrayList<ProblemPageListVO> problemPageListVOS = new ArrayList<>();
        for (ProblemDO record : page.getRecords()) {
            ProblemPageListVO problemPageListVO = new ProblemPageListVO();
            BeanUtils.copyProperties(record,problemPageListVO);

            problemPageListVO.setUserName(longUserBasicInfoDTOMap.get(record.getUserId()).getNickname());

            problemPageListVOS.add(problemPageListVO);
        }
        return new PageResult<>(page.getTotal(),problemPageListVOS);
    }

    public ProblemDTO info(String problemId) {
        ProblemDO problemDO = problemDao.getById(problemId);
        return ProblemDO.toDTO(problemDO);
    }

    public ProblemVO getInfoVO(String id) {
        ProblemDO problemDO = problemDao.getById(id);
        ProblemDTO problemDTO = ProblemDO.toDTO(problemDO);

        ProblemVO problemVO = new ProblemVO();
        BeanUtils.copyProperties(problemDTO,problemVO);

        return problemVO;
    }

    public Map<String, ProblemDTO> idsMapDTO(List<String> problemIds) {
        List<ProblemDTO> problemDTOS = queryByIds(problemIds);
        Map<String, ProblemDTO> dtoHashMap = new HashMap<>();
        for (ProblemDTO problemDTO : problemDTOS) {
            dtoHashMap.put(problemDTO.getId(),problemDTO);
        }
        return dtoHashMap;
    }
}
